package swd.maintain.project.prg_in_sys_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);


      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"SWD",view,business);

      String lstrResult="";
      try{

         // if(view.getsprg_no()!=null && view.getsprg_no().length>0){
         if (view.getprg_no()!=null && !view.getprg_no().equals("")){
             view.setSelectShow("");
         }else{
              view.setSelectShow("none");
         }
       //  } else {
        //    view.setSelectShow("none");
       // }
           view.setButtonVisible("0");
           view.setins_sys_no_readonly(true);
           view.setins_sys_name_readonly(true);




       if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
            business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));

       }
       if(view.getPrgAction()!=null && view.getPrgAction().equals("update")){
            business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));

       }

       // Save
       if(view.getPrgAction()!=null && view.getPrgAction().equals("save") ){
          //lstrResult= flow.addAction();
          view.setOnloadScript("alert('" + lstrResult + "');");
          if (lstrResult.equals("資料新增成功!!")){
           view.setButtonVisible("2");
           view.setins_sys_no("");

           view.setins_sys_name("");

          } else {
             view.setButtonVisible("1");
             view.setins_sys_name("");
             view.setins_sys_no("");

          }
       }
       // Add
        if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
           view.setAllClear();
           view.setins_sys_no_readonly(false);
           view.setins_sys_name_readonly(true);

           view.setButtonVisible("1");
           view.setPrgStatus("add");
        }

        // 離開
       if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
           if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add"))){
               view.setAllClear();
               view.setButtonVisible("0");
               view.setPrgStatus("exit");
           } else {
              view.setAllClear();
              view.setSelectShow("none");
              view.setPrgStatus("exit");
           }
       }





       business.setDataViewForm(DataViewForm,view);
         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT "+e.toString()+"');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
